<html>
  <head>
	  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>i5ting_ztree_toc:05</title>
		<link href="toc/style/github-bf51422f4bb36427d391e4b75a1daa083c2d840e.css" media="all" rel="stylesheet" type="text/css"/>
		<link href="toc/style/github2-d731afd4f624c99a4b19ad69f3083cd6d02b81d5.css" media="all" rel="stylesheet" type="text/css"/>
		<link href="toc/css/zTreeStyle/zTreeStyle.css" media="all" rel="stylesheet" type="text/css"/>
	  <style>
		pre {
		    counter-reset: line-numbering;
		    border: solid 1px #d9d9d9;
		    border-radius: 0;
		    background: #fff;
		    padding: 0;
		    line-height: 23px;
		    margin-bottom: 30px;
		    white-space: pre;
		    overflow-x: auto;
		    word-break: inherit;
		    word-wrap: inherit;
		}

		pre a::before {
		  content: counter(line-numbering);
		  counter-increment: line-numbering;
		  padding-right: 1em; /* space after numbers */
		  width: 25px;
		  text-align: right;
		  opacity: 0.7;
		  display: inline-block;
		  color: #aaa;
		  background: #eee;
		  margin-right: 16px;
		  padding: 2px 10px;
		  font-size: 13px;
		  -webkit-touch-callout: none;
		  -webkit-user-select: none;
		  -khtml-user-select: none;
		  -moz-user-select: none;
		  -ms-user-select: none;
		  user-select: none;
		}

		pre a:first-of-type::before {
		  padding-top: 10px;
		}

		pre a:last-of-type::before {
		  padding-bottom: 10px;
		}

		pre a:only-of-type::before {
		  padding: 10px;
		}

		.highlight { background-color: #ffffcc } /* RIGHT */
		</style>
  </head>
  <body>
	  <div>
				<div style='width:25%;'>
						<ul id="tree" class="ztree" style='width:100%'>

						</ul>
				</div>
        <div id='readme' style='width:70%;margin-left:20%;'>
          	<article class='markdown-body'>
            	<h1 id="05-">05. 替换空格</h1>
<p>难度：<strong>简单</strong></p>
<p>请实现一个函数，把字符串 s 中的每个空格替换成&quot;%20&quot;。</p>
<p>示例 1：</p>
<pre><code>输入：s = &quot;We are happy.&quot;

输出：&quot;We%20are%20happy.&quot;
</code></pre><p>限制：</p>
<p>0 &lt;= s 的长度 &lt;= 10000</p>
<p>来源：力扣（LeetCode）</p>
<p>链接：<a href="https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof">https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof</a></p>
<p>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<hr>
<p>解法1： Java ，转char 数组，String Builder拼接</p>
<pre><code class="lang-java">public String replaceSpace(String s) {

    StringBuffer resSb = new StringBuffer();

    char[] sc=s.toCharArray();

    for(int i = 0;i&lt;sc.length;i++){
        if(sc[i]==&#39; &#39;){
            resSb.append(&quot;%20&quot;);
        }else{
            resSb.append(sc[i]);
        }
    }

    return resSb.toString();
}
</code></pre>
<p>解法2： C,先扩充数组，再使用双指针</p>
<pre><code class="lang-c">class Solution {
public:
    string replaceSpace(string s) {
        int count = 0, len = s.size();
        // 统计空格数量
        for (char c : s) {
            if (c == &#39; &#39;) count++;
        }

        // 修改 s 长度
        s.resize(len + 2 * count);

        // 倒序遍历修改
        for(int i = len - 1, j = s.size() - 1; i &lt; j; i--, j--) {
            //双指针  
            if (s[i] != &#39; &#39;)s[j] = s\[i];
            else {
                s[j - 2] = &#39;%&#39;;
                s[j - 1] = &#39;2&#39;;
                s[j] = &#39;0&#39;;
                j -= 2; // j要往后退2个位置
            }
        }
        return s;
    }
};
</code></pre>

          	</article>
        </div>
		</div>
  </body>
</html>
<script type="text/javascript" src="toc/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="toc/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="toc/js/ztree_toc.js"></script>
<script type="text/javascript" src="toc_conf.js"></script>

<SCRIPT type="text/javascript" >
<!--
$(document).ready(function(){
    var css_conf = eval(markdown_panel_style);
    $('#readme').css(css_conf)
    
    var conf = eval(jquery_ztree_toc_opts);
		$('#tree').ztree_toc(conf);
});
//-->
</SCRIPT>